﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LEARNREN.COM.UI;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using LEARNREN.COM.Entity.Enum;
using LEARNREN.COM.TOOL;

namespace LEARNREN.COM.WebSite.Administrator
{
    public partial class ProductList : BasePage
    {
        private ProductLogic productLogic = null;
        private CatalogueLogic catalogueLogic = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitProductList(1);
                InitTree();
            }
        }

        /// <summary>
        /// 初始化树
        /// </summary>
        private void InitTree()
        {
            this.CatalogueTree1.CatalogueLogicProvider = catalogueLogic;
            this.CatalogueTree1.CanClickNodeType = CatalogueTypeEnum.ProductList;
        }

        /// <summary>
        /// 选择节点改变
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CatalogueTree1_SelectedNodeChanged(object sender, DelegateAndArges.CatalogueTreeSelectedNodeChangedEventArgs e)
        {
            ViewState["CatalogueId"] = e.SelectedCatalogueId;
            InitProductList(1);
        }

        /// <summary>
        /// 载入产品列表
        /// </summary>
        /// <param name="pageIndex"></param>
        private void InitProductList(int pageIndex)
        {
            object sid = ViewState["CatalogueId"];
            List<Product> productList = null;
            if (sid == null)
            {
                productList = productLogic.GetList().OrderByDescending(p => p.Id).ToList();
            }
            else
            {
                long id = 0;
                if (long.TryParse(sid.ToString(), out id))
                {
                    productList = productLogic.GetList(p => p.CatalogueId == id).OrderByDescending(p => p.Id).ToList();
                }
                else
                {
                    productList = new List<Product>();
                }
            }
            this.gvProducts.PageIndex = pageIndex;
            this.gvProducts.DataSource = productList;
            this.gvProducts.DataBind();
        }

        /// <summary>
        /// 获取要显示的正确 页码数
        /// </summary>
        /// <param name="oldPageIndex"></param>
        /// <returns></returns>
        private int GetShowPageIndex(int oldPageIndex)
        {
            return this.gvProducts.PageCount < oldPageIndex ? this.gvProducts.PageCount : oldPageIndex;
        }

        protected void gvProducts_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            InitProductList(e.NewPageIndex);
        }

        protected void gvProducts_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                int oldPageIndex = this.gvProducts.PageIndex;

                long id = (long)e.Keys[0];
                Product product = productLogic.GetById(id);
                if (product != null)
                {
                    productLogic.Delete(product);
                }

                AddScript("alert('删除成功！');");
                InitProductList(GetShowPageIndex(oldPageIndex));
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                AddScript("alert('删除失败！');");
            }
        }

        protected override void InitLogic()
        {
            productLogic = new ProductLogic();
            catalogueLogic = new CatalogueLogic();
        }

        protected override void DisposeLogic()
        {
            if (productLogic != null)
            {
                productLogic.Dispose();
                productLogic = null;
            }
            if (catalogueLogic != null)
            {
                catalogueLogic.Dispose();
                catalogueLogic = null;
            }
        }
    }
}